package com.ztesoft.web.resourcecheck;

import org.springframework.stereotype.Component;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.Session;
import com.ztesoft.core.protocol.client.JSchClient;
import com.ztesoft.framework.exception.BaseAppException;
import com.ztesoft.framework.exception.ExceptionHandler;
import com.ztesoft.framework.log.ZTEsoftLogManager;
import com.ztesoft.web.common.DMSConstant;
import com.ztesoft.web.common.Util;
import com.ztesoft.web.common.vo.HostInfoPOJO;

/**
 * SSH登录方式执行命令监测<br>
 * 
 * @author <a href="mailto:lin.weibin@zte.com.cn">lin.wb</a> 2016年5月5日 下午4:56:28
 * @version 1.0
 */
@Component
public class SSHCheckStrategy extends CheckStrategy{
	

	private static final ZTEsoftLogManager LOGGER = ZTEsoftLogManager
			.getLogger(SSHCheckStrategy.class);
	
	@Override
	public String check(HostInfoPOJO hostInfoPOJO, int checkType)throws BaseAppException {
		//对结果做处理 很重要！！！
		return Util.dealResult(this.execNormalCommand(hostInfoPOJO, Util.getExecCmd(hostInfoPOJO.getOsType(), checkType)));
	}
	@Override
	public String checkT(HostInfoPOJO hostInfoPOJO, String shellCommand)throws BaseAppException {
		return Util.dealResultT(this.execNormalCommand(hostInfoPOJO, shellCommand));
	}

	@Override
	public String checkIfxLMode(HostInfoPOJO hostInfoPOJO, String dbName,
			String tableName) throws BaseAppException {
		String execCmd = DMSConstant.IFX_LMODE_CMD;
		execCmd = execCmd.replaceAll("\\{0\\}",dbName);//
		execCmd = execCmd.replaceAll("\\{1\\}",tableName);//
		return this.execNormalCommand(hostInfoPOJO, execCmd);
	}

	
	/**
	 * 命令执行execNormalCommand |||此处获取一行结果，有特殊需求完善处理 TODO
	 * @param hostInfoPOJO
	 * @param execCmd
	 * @return
	 * @throws BaseAppException
	 */
	private String execNormalCommand(HostInfoPOJO hostInfoPOJO, String execCmd) throws BaseAppException {
				
		// 
		long tt = System.currentTimeMillis();
		Session session = null;
		ChannelExec channelExec = null;
		try {
			session = JSchClient.creatSession(hostInfoPOJO);
			channelExec = (ChannelExec) JSchClient
					.getExecChannel(session);
			
			String execResult = JSchClient.execNormalCommand(channelExec, execCmd);
			if("".equals(execResult)) {
				LOGGER.info("JSchClient.execNormalCommand执行获取不到数据改用ChannelShell尝试获取数据..");
				execResult = this.execShellCommand(session, execCmd);
			}
			LOGGER.info("耗时{0},查询{1},结果:{2}",(System.currentTimeMillis()-tt)+"ms", execCmd, execResult);
			return execResult;
		} catch (Exception e) {
			// 
			LOGGER.error("ssh方式执行监测异常{0}",execCmd);
			throw ExceptionHandler.publish("ssh方式执行监测异常", e);
		} finally {
			JSchClient.closeChannel(channelExec);
			JSchClient.returnSession(hostInfoPOJO, session);
		}
	}
	
	private String execShellCommand(Session session, String execCmd) throws BaseAppException {
		
		ChannelShell channelShell = null;
		try {
			channelShell = (ChannelShell) JSchClient.getShellChannel(session);
			return JSchClient.execShellCommand(channelShell, execCmd);
		} catch (Exception e) {
			throw ExceptionHandler.publish("ssh方式执行命令异常", e);
		} finally {
			JSchClient.closeChannel(channelShell);
		}
	}
}
